package org.itboys.mobile.service.mongo.weixin;


import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.ZhongBaoMessage;
import org.itboys.mobile.entity.mongo.project.ZhongBaoProject;
import org.itboys.mobile.entity.mongo.project.ZhongBaoProjectSign;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：众包_微信_报名_service
 */
@Service
public class ZhongBaoWeiXinProjectSignService extends BaseMongoService<ZhongBaoProjectSign> {

    @Autowired
    private SundayWeiXinMemberService memberService;
    @Autowired
    private ZhongBaoWeiXinProjectService projectService;
    @Autowired
    private ZhongBaoWeiXinMessageService messageService;
    @Resource(name = "mobileDs")
    private MongoDataSource mobileDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mobileDataSource;
    }

    @Override
    protected Class<ZhongBaoProjectSign> getEntityClass() {
        return ZhongBaoProjectSign.class;
    }

    /**
     *  报名或取消报名
     * @param projectId
     * @param memberId
     */
    public void sign(Long projectId,Long memberId,String remark){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("projectId",projectId);
        param.put("memberId",memberId);
        param.put("status in",new Integer[]{SundayCommonConstants.zhongbao_project_sign_dsh,SundayCommonConstants.zhongbao_project_sign_ytg});

        if(super.count(param)>0){
            //取消报名
            super.delete(param);
        }else{
            //申请报名需要审核
             SundayMember member=memberService.getById(memberId);
             ZhongBaoProject project=projectService.getById(projectId);
            //生成报名信息
            ZhongBaoProjectSign sign= new ZhongBaoProjectSign( projectId,  memberId,  member.getNickName(),  member.getMobile(),  member.getImage(), remark,  SundayCommonConstants.zhongbao_project_sign_dsh);
            super.save(sign);
            //生成审核消息
            messageService.generateMessage(project.getMemberId(),memberId,projectId,SundayCommonConstants.zhongbao_message_type_sign_apply,"报名申请",remark,sign.getId(),null);

        }
    }

    /**
     *校验是否转发、报名、关注
     * @param projectId
     * @param memberId
     */
    public Integer check(Long projectId,Long memberId){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("projectId",projectId);
        param.put("memberId",memberId);
        //param.put("status in",new Integer[]{SundayCommonConstants.zhongbao_project_sign_dsh,SundayCommonConstants.zhongbao_project_sign_ytg});
       List<ZhongBaoProjectSign> signs=super.list(param);
       if(ListUtil.isNull(signs)){
           return 0;
       }else{
           ZhongBaoProjectSign sign= signs.get(0);
           //正在审核
           if(sign.getStatus()==SundayCommonConstants.zhongbao_project_sign_dsh){
               return 2;
           //已通过（已报名）
           }else if(sign.getStatus()==SundayCommonConstants.zhongbao_project_sign_dsh){
               return 1;
            //未通过（未报名）
           }else{
               return 0;
           }

       }
    }

    /**
     *获取报名人数
     * @param projectId
     * @param memberId
     */
    public List<ZhongBaoProjectSign> select(HttpServletRequest request, Long projectId, Long memberId){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("projectId",projectId);
        param.put("status",SundayCommonConstants.zhongbao_project_sign_ytg);
        if(LongUtil.isNotZreo(memberId)){
            param.put("memberId",memberId);
        }
        return super.pageQuery(request,param).getData();
    }
    /**
     *获取报名人数。不分页获取
     * @param projectId

     */
    public List<ZhongBaoProjectSign> selectNoPage( Long projectId){
        Map<String,Object> param=new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("projectId",projectId);
        param.put("status",SundayCommonConstants.zhongbao_project_sign_ytg);

        return super.list(param);
    }
    /**
     * 处理报名信息
     * @param memberId
     * @param messageId
     * @return
     */
   public void handle(Long projectId, Long memberId,Long messageId,boolean isAgree){
       ZhongBaoProject project =projectService.getById(projectId);
       //校验用户
       if(project.getMemberId().longValue()!=memberId.longValue()){
           throw new ErrorException("系统开小差了，请稍后重试");
       }
        //获取通知消息（内含报名信息）
       ZhongBaoMessage message=messageService.getById(messageId);
       //报名记录的Id
       Long signId=message.getOtherId();
        //审核结果。默认为
       Integer signStatus=isAgree?SundayCommonConstants.zhongbao_project_sign_ytg:SundayCommonConstants.zhongbao_project_sign_ybh;
       String messageMsg=isAgree?"您的项目["+project.getName()+"]报名申请已通过。":"您的项目["+project.getName()+"]报名申请已经被驳回，可以重新申请。";

        //修改报名记录
       super.update(signId,"status",signStatus);

        //发送消息给用户
       messageService.generateMessage(message.getSenderId(),memberId,projectId,SundayCommonConstants.zhongbao_message_type_sign_pass,"报名审核通知",messageMsg,signId,messageId);

   }

}
